
#import "CanadianObject.h"

@implementation CanadianObject


// AWAKEFROMNIB
// This is one of the last methods executed after the nib file is loaded.
// This is the BEST way I know how to initialize an object and jump into
// a method after the nib file was loaded, because you are all the methods
// and variables needed can be used. (Unlike init which is not compeletly
// ready when that message is sent
- awakeFromNib
{
 
	[CalcWindow makeKeyAndOrderFront:NULL];
	[StockPrice selectText:self];

    	return self;
}





- Calc:sender
{

	//Assign Misc Variables
    	char dummyString[20];
    	int WhichOne;

    	
	//Assign CalcWindow Variables
	float stockprice;
    	float numberofshares;
    	float totalstockprice;
    	float totalcommission;
    	float total;
	float netamountpershare;

	stockprice = [StockPrice floatValue];
	numberofshares = [NumberOfShares floatValue];


	// Take the value in Stock Price * Number of Shares and display
	// the result in the Total Stock Price textfield
	totalstockprice = [StockPrice floatValue] * [NumberOfShares floatValue];
	sprintf(dummyString, "%.2f", totalstockprice); 
	[TotalStockPrice setStringValue:dummyString];// Display result


// Figure out the commission rate and then do the calculations

// Checks if the stock price is below Stock1 if yes then
// calculates the commission using Comm1
if ([StockPrice floatValue] <= [Stock2 floatValue]) totalcommission = (([StockPrice floatValue] * [NumberOfShares floatValue]) * ([Comm1 floatValue] / 100));

// Checks if the stock price is >=Stock3 and <= Stock4 if yes then
// calculates the commission by adding Comm2 and multiplying
// Comm3 by the number of shares
if ([StockPrice floatValue] >= [Stock3 floatValue] && [StockPrice floatValue] <= [Stock4 floatValue]) totalcommission = ([Comm2 floatValue] + ([NumberOfShares floatValue] * [Comm3 floatValue]));

// Checks if the stock price is >=Stock5 and <= Stock6 if yes then
// calculates the commission by adding Comm4 and multiplying
// Comm5 by the number of shares
if ([StockPrice floatValue] >= [Stock5 floatValue] && [StockPrice floatValue] <= [Stock6 floatValue]) totalcommission = ([Comm4 floatValue] + ([NumberOfShares floatValue] * [Comm5 floatValue]));

// Checks if the stock price is >=Stock7 and <= Stock8 if yes then
// calculates the commission by adding Comm6 and multiplying
// Comm7 by the number of shares
if ([StockPrice floatValue] >= [Stock7 floatValue] && [StockPrice floatValue] <= [Stock8 floatValue]) totalcommission = ([Comm6 floatValue] + ([NumberOfShares floatValue] * [Comm7 floatValue]));

// Checks if the stock price is >=Stock9 and <= Stock10 if yes then
// calculates the commission by adding Comm8 and multiplying
// Comm9 by the number of shares
if ([StockPrice floatValue] >= [Stock9 floatValue] && [StockPrice floatValue] <= [Stock10 floatValue]) totalcommission = ([Comm8 floatValue] + ([NumberOfShares floatValue] * [Comm9 floatValue]));

// Checks if the stock price is >=Stock11 and <= Stock12 if yes then
// calculates the commission by adding Comm10 and multiplying
// Comm11 by the number of shares
if ([StockPrice floatValue] >= [Stock11 floatValue] && [StockPrice floatValue] <= [Stock12 floatValue]) totalcommission = ([Comm10 floatValue] + ([NumberOfShares floatValue] * [Comm11 floatValue]));

// Checks if the stock price is >=Stock13 if yes then
// calculates the commission by adding Comm11 and multiplying
// Comm13 by the number of shares
if ([StockPrice floatValue] >= [Stock13 floatValue]) totalcommission = ([Comm12 floatValue] + ([NumberOfShares floatValue] * [Comm13 floatValue]));


	// Assign variables
	totalstockprice = [TotalStockPrice floatValue];

	// Transactions with principal values less than Principal Value 
        // will be charged a flat fee of FlatFee
        if ([TotalStockPrice floatValue] <= [PrincipalValue floatValue]) totalcommission = [FlatFee floatValue];


	// Assigns the value of the currently selected radio button
 	// using tag as a reference and calculate Net Amount
   	WhichOne = [RadioButton selectedTag]; 
	if (WhichOne == 2)  totalcommission = totalcommission -(totalcommission * ([TeleMaxInput floatValue] / 100));

	// Assigns the value of the currently selected radio button
 	// using tag as a reference
   	WhichOne = [BuySellRadio selectedTag]; 
	if (WhichOne == 1)  total = totalstockprice + totalcommission;
	if (WhichOne == 2)  total = totalstockprice - totalcommission;

	
	// Display Total Commission 
	// Convert float to a string
	sprintf(dummyString, "%.2f", totalcommission); 
	[TotalCommission setStringValue:dummyString];// Display result

	// Display Net Amount
	sprintf(dummyString, "%.2f", total); // Convert float to a string
	[Total setStringValue:dummyString];// Display result

	// Calculate Net Amount Per Share
	// Number of Shares / Net Amount = Net Amount per Share
	netamountpershare =  [Total floatValue] / [NumberOfShares floatValue];
	// Display Net Amount per Share
	sprintf(dummyString, "%.2f", netamountpershare);
	[NetAmountPerShare setStringValue:dummyString];// Display result
	[NetAmountPerShare display];


	[StockPrice selectText:self];


    return self;
}

- DirectTrading:sender
{
    return self;
}

- PrintCalc:sender
{

	[CalcWindow printPSCode:self];

    return self;
}

- PrintCommSchedule:sender
{

	[CommissionWindow printPSCode:self];

    return self;
}

- TeleMax:sender
{
    return self;
}


@end
